package com.zpark.it.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpark.it.pojo.Student;
import com.zpark.it.service.StudentService;
import com.zpark.it.utils.MD5;
import com.zpark.it.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: StudentController
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/31 17:21
 */
@Api(tags = "学生管理控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation("分页带条件查询学生信息")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(@ApiParam("分页查询的页码") @PathVariable Integer pageNo,
                                  @ApiParam("分页查询的页码大小")@PathVariable Integer pageSize,
                                 @ApiParam("查询条件") Student student){

        Page<Student> page=new Page<>(pageNo,pageSize);
        IPage<Student> iPage = studentService.getStudentByOpr(page, student);
        return Result.ok(iPage);
    }

    @ApiOperation("删除多个和多个学生信息")
    @DeleteMapping("delStudentById")
    public Result delStudentById(@ApiParam("要删除的多个学生的ID的json数组")@RequestBody List<Integer> ids){
         studentService.removeByIds(ids);
         return Result.ok();
    }

    /**
     * 增加学生信息
     * @param student
     * @return
     */
    @ApiOperation("保存或者修改学生信息")
    @PostMapping("addOrUpdateStudent")
    public Result addOrUpdateStudent(@ApiParam("要保存或修改的json格式的学生")@RequestBody Student student){
        Integer id = student.getId();
        if (id==null||id==0){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return  Result.ok();
    }


}
